<template>
  <div class="h100 lookupTestDetailMainListsComp">
    <full-parent-dom-with-dask :loading="changingUpdateStatus"></full-parent-dom-with-dask>
    <s-table
      :columns="columns"
      :data="loadData"
      :scrollX="1040"
      rowKey="id"
      class="h100"
      ref="table"
      :rowClassName="rowClassName"
    >
      <template #vin="text">
        <table-ellipsis :text="text"></table-ellipsis>
      </template>
      <template #status="text">
        <table-ellipsis :text="taskVehicleStatusDictCodeObj[text]"></table-ellipsis>
      </template>
      <template #startTime="text">
        <table-ellipsis :text="$commonFun.getDateMinuteStr(text)"></table-ellipsis>
      </template>
      <template #updateTime="text">
        <table-ellipsis :text="$commonFun.getDateTimeStr(text)"></table-ellipsis>
      </template>
      
      <!-- <template #upgradeStatus="text">
        <table-ellipsis :text="taskVehicleUpgradeStatusDictCodeObj[text]"></table-ellipsis>
      </template> -->
      <!-- <template #vehicleModel="text">
        <table-ellipsis :text="text || '-'"></table-ellipsis>
      </template> -->
      <!-- <template #stereotype="text">
        <table-ellipsis :text="text || '-'"></table-ellipsis>
      </template> -->
      <template #action="text, record">
        <template v-if="record.deleted">-</template>
        <operation-menu
          v-else
          :menus="getTbRowMenus(record)"
          @clickaction="(key) => {
            doAction(key, record)
          }"
        ></operation-menu>
      </template>
    </s-table>
    <!-- 零件升级详情 -->
    <ecu-update-detail-dialog
      ref="ecuUpdateDetailDialogRef"
      height="70vh"
      width="80%"
      :query="partDialogQuery">
    </ecu-update-detail-dialog>
    <!-- 应用软件升级详情 -->
    <app-update-detail-dialog
      v-if="appDialogQuery"
      ref="appUpdateDetailDialogRef"
      height="60vh"
      width="70%"
      :query="appDialogQuery"
    ></app-update-detail-dialog>
    <!-- 异常日志 -->
    <part-update-exception-log-dialog
      v-if="exceptionDialogQuery"
      ref="partUpdateExceptionLogDialog"
      height="60vh"
      width="60%"
      :query="exceptionDialogQuery"
      :time="time"
    ></part-update-exception-log-dialog>
  </div>
</template>

<script>
import { strategyViewTestVehicleColumns } from '../config/list'
import tableEllipsis from '@/components/Ellipsis/TableEllipsis'
import OperationMenu from '@/components/OperationMenu'
import EcuUpdateDetailDialog from '@/views/components/EcuUpdateDetail/dialog.vue'
import AppUpdateDetailDialog from '@/views/components/OneAppUpdateDetail/dialog.vue'
import PartUpdateExceptionLogDialog from '@/views/components/PartUpdateExceptionLog/dialog.vue'
import FullParentDomWithDask from '@/components/LoadDataSpin/FullParentDomWithDask.vue'
import { mapState } from 'vuex'
import { STable } from '@/components'
export default {
  components: {
    tableEllipsis,
    OperationMenu,
    EcuUpdateDetailDialog,
    PartUpdateExceptionLogDialog,
    STable,
    FullParentDomWithDask,
    AppUpdateDetailDialog
  },
  props: {
    upgradeTaskId: {
      type: String,
      default: ''
    },
    vin:{
      type:String,
      default:''
    },
    status:{
      type:String,
      default:''
    },
    endTime: {
      type: Number,
      default: undefined
    },
    taskId: {
      type: String,
      default: ''
    },
    loadingData: {
      type: Boolean,
      default: false
    },
    // 是sota测试任务8,fota测试任务9
    type: {
      type: Number,
      default: 9
    },
    upgradeTriggerType: {
      type: String,
      default: ''
    },
    update: {
      type: Number,
      default: 0
    }
  },
  computed: {
    taskVehicleStatusDictCodeObj () {
      return this.$store.getters.getDictCodeObj('taskVehicleStatusDictCode')
    },
    taskVehicleUpgradeStatusDictCodeObj () {
      return this.$store.getters.getDictCodeObj('taskVehicleUpgradeStatusDictCode')
    },
    ...mapState({
      queryParams: (state) => state.strategy.taskVehicleQueryParams
    })
  },
  data () {
    const _this = this
    return { 
      columns: this.upgradeTriggerType==='AUTO'? strategyViewTestVehicleColumns:strategyViewTestVehicleColumns.filter(item=>item.dataIndex!=='factoryRetryCount'),
      tbRowMenus: [
        {
          title: this.$t('partUpdateDetail'),
          key: 'partUpdateDetail',
          disabled () {
            return _this.type !== 9
          }
        },
        {
          title: this.$t('exceptionLog'),
          key: 'exceptionLog'
        },
        {
          title: this.$t('applicationDetail'),
          key: 'applicationDetail',
          disabled () {
            return _this.type !== 8
          }
        },
        // {
        //   title: this.$t('ztsj'),
        //   key: 'ztsj',
        //   // 暂停升级(上报状态为未开始1或者进行中2且操作状态为正常时才有)
        //   disabled (record) {
        //     return !((record.status === 1 || record.status === 2) && record.upgradeStatus === 1) || _this.taskInfo.channel === 0
        //   }
        // },
        {
          title: this.$t('cancelUpgrade'),
          key: 'cancelUpgrade',
          // 取消升级 上报状态为未开始1或者进行中2且操作状态为正常时才有
          disabled (record) {
            return !((record.status === 1 || record.status === 2) && record.upgradeStatus === 1) || _this.taskInfo.channel === 0
          }
        },
        // {
        //   title: this.$t('resumeUpgrade'),
        //   key: 'renewUpgrade',
        //   // 恢复升级 操作状态为暂停升级2
        //   disabled (record) {
        //     return !(record.upgradeStatus === 2) || _this.taskInfo.channel === 0
        //   }
        // }
      ],
      // 查看零件详情的搜索条件
      partDialogQuery: null,
      // 查看异常日志的搜索条件
      exceptionDialogQuery: null,
      // 查看软件详情搜索条件
      appDialogQuery: null,
      actions: {
        partUpdateDetail: {
          method: 'showEcuUpdateDetail'
        },
        exceptionLog: {
          method: 'showEcuExceptionDetail'
        },
        // applicationDetail: {
        //   method: 'showAppUpdateDetail'
        // },
        // ztsj: {
        //   method: 'stopUpdate'
        // },
        cancelUpgrade: {
          method: 'cancelUpdate'
        },
        // renewUpgrade: {
        //   method: 'renewUpgrade'
        // }
      },
      // 是否正在暂停或者取消升级
      changingUpdateStatus: false,
      taskInfo: {},
      time: 456258,
      timestamp: new Date().getTime()
    }
  },
  watch: {
    vin:{
      handler (val) {
        this.refreshData(true)
      }
    },
    status:{
      handler (val) {
        this.refreshData(true)
      }
    },
    'queryParams.taskId': {
      handler (val) {
        if (!val) {
          return
        }
        this.$nextTick(() => {
          this.refreshData(true)
        })
      }
    },
    taskId() {
      this.getTaskInfo();
    },
    update: {
      handler () {
        this.refreshData()
      }
    }
  },
  created () {
    this.getPageDictData()
    this.getTaskInfo()
  },
  methods: {
    getTaskInfo () {
      const vm = this
      vm.$api.getTaskInfor({ id: vm.taskId }).then((res) => {
         vm.taskInfo = res.data || {}
      })
    },
    // 获取操作每行的菜单
    getTbRowMenus (record) {
      if (this.timestamp > this.endTime) {
        return this.tbRowMenus.filter(item => {
          return !((item.disabled && item.disabled(record)) || item.key === 'cancelUpgrade' || item.title === this.$t('ztsj'))
        })
      } else {
        return this.tbRowMenus.filter( item => {
          return !(item.disabled && item.disabled(record))
        })
      }
      
    },
    doAction (key, record) {
      const method = this[this.actions[key].method]
      if (method) {
        method(record)
      }
    },
    // 查看零件详情
    showEcuUpdateDetail (record) {
      this.$router.push({path:"/statistics/monitorList","query":{
         vin: record.vin,
         taskId: record.taskId
       }})
    },
    // 查看异常日志showEcuExceptionDetail
    showEcuExceptionDetail (record) {
      this.time = Date.now()
      this.exceptionDialogQuery = {
        vehicleId: record.vehicleId,
        taskIds: record.taskId
      }
      this.$nextTick(() => {
        this.$refs.partUpdateExceptionLogDialog.switchVisible(true)
      })
    },
    // 查看应用详情
    showAppUpdateDetail (record) {
      this.appDialogQuery = {
        vehicleId: record.vehicleId,
        softwareDid: record.softwareDid,
        taskIds: record.taskId
      }
      this.$nextTick(() => {
        this.$refs.appUpdateDetailDialogRef.switchVisible(true)
      })
    },
    // 获取任务对应的车辆列表
    loadData (parameter) {
      if (!this.taskId) {
        return new Promise((resolve) => {
          resolve({
            code: 0,
            message: '',
            data: [],
            totalCount: 0,
            totalPage: 0
          })
        })
      }
      const query = {
        ...parameter,
        ...this.queryParams,
        vin:this.vin,
        taskId:this.upgradeTaskId?this.upgradeTaskId:this.taskId,
        status:this.status
      }
      return this.$store.dispatch('getTaskVehiclePageData', query)
    },
    refreshData (isToFirstPage = true) {
      let obj = null
      if (isToFirstPage) {
        obj = { current: 1 }
      }
      this.$refs.table.loadData(obj)
    },
    // 暂停升级
    stopUpdate (record) {
      this.confirm(this.$t('ztsj'), record.vin, () => {
        this.upgradeStatusTaskCars(record, 2)
      })
    },
    // 取消升级
    cancelUpdate (record) {
      this.confirm(this.$t('cancelUpgrade'), record.vin, () => {
        this.upgradeStatusTaskCars(record, 3)
      })
    },
    // 恢复升级
    renewUpgrade (record) {
      this.confirm(this.$t('renewUpgrade'), record.vin, () => {
        this.upgradeStatusTaskCars(record, 1)
      })
    },
    confirm (actionName, vehicleVin, onOk) {
      const vm = this
      this.$confirm({
        title: vm.$t('systemTip'),
        content: vm.$t('taskVehicleUpgradeActionTip').replace('__CONTENT__', actionName) + `[${vehicleVin}]?`,
        onOk,
        onCancel () { }
      })
    },
    // 暂停或取消升级
    upgradeStatusTaskCars (record, status) {
      const vm = this
      vm.changingUpdateStatus = true
      vm.$api.upgradeStatusTaskCars({
        taskId: [record.taskId],
        taskVehicleId: record.vehicleId,
        upgradeStatus: status
      }).then(() => {
        vm.$message.success(vm.$t('successfullyOperation'))
        vm.refreshData()
      }).finally(() => {
        vm.changingUpdateStatus = false
      })
    },
    getPageDictData () {
      ['getTaskVehicleStatusDictCodeData', 'getTaskVehicleUpgradeStatusDictCodeData'].forEach(method => {
        this.$store.dispatch(method)
      })
    },
    rowClassName (record) {
      return record.deleted ? 'rowRemoved' : ''
    }
  }
}
</script>
<style lang="less" scoped>
.lookupTestDetailMainListsComp {
  position: relative;
}
</style>
